<script language=javascript>
function new_cat()
{
	if (document.getElementById('new').selected == true)
		document.getElementById('i_new').style.display = "block";
	else
		document.getElementById('i_new').style.display = "none";
	//	if (document.getlementbyid('new').style.display == none)
	//	document.getelementbyid('new').style.display == block;
}

</script>
<?php
if (isset($_GET['show_mag_id']))
	$idmag = $_GET['show_mag_id'];
if (isset($_POST['mag_id']))
	$idmag = $_POST['mag_id'];
if (isset($_GET['action']) and $_GET['action'] == "edit")
{
	if (isset($_POST['prod_name']) AND isset($_POST['prod_price']) 
			AND isset($_POST['prod_tva']) AND isset($_POST['prod_cel'])
				AND isset($_POST['prod_max']) AND isset($_POST['prod_code']))
		{
		if (is_numeric($_POST['prod_price']) AND is_numeric($_POST['prod_tva'])
				AND is_numeric($_POST['prod_max']))
			{
				global $connect;
				$query = "call update_produit( '".$_POST['prod_id']."' , 
					'".$_POST['prod_name']."' , 
					'".$_POST['prod_code']."' ,
					'".$_POST['prod_cat']."' , 
					'".$_POST['prod_price']."',
					'".$_POST['prod_tva']."' ,
					'".$_POST['prod_cel']."'  ,
					'".$_POST['prod_four']."' ,
					'".$_POST['prod_four_price']."' ,
					'".$_POST['prod_tva_four']."' ,
					'".$_POST['prod_max']."');";
				mysql_query($query, $connect) or die(mysql_error());
				echo "<div class='box_success'>Produit mis &agrave; jour</div><br />";
			}
			else
				echo "<p class='box_error'>Attention certains champs ne prennent que des chiffres</p> <br />";
			
		}
		else
			echo "<p class='box_error'>Certains champs n'ont pas &eacute;t&eacute; remplis </p> <br />";
		
}
else
			echo "<p class='box_error'> grrrr </p> <br />";
?>
<div id="ventes">
	<div id="prd_list">	
		<center>
		<h1>Modification d'un produit</h1><hr />
			<form action="index.php?page=produits&action=edit&edit_prod_id=<?php echo $_GET['edit_prod_id'];?>" method="post">
				<table  border="0" cellspacing="8" cellpadding="40" width="400px">
				<?php
			global $connect;
			$query = "SELECT c.nomcategorie AS NOMCATEGORIE, c.ncategorie AS NCATEGORIE FROM categories c LEFT Join produits p ON (p.ncategorie = c.ncategorie)
							WHERE p.nproduit  = '".$_GET['edit_prod_id']."'";
			$query2 = "SELECT * FROM categories";
			$query3 = "SELECT * FROM produits WHERE NPRODUIT ='".$_GET['edit_prod_id']."'";
			$query4 = "SELECT * FROM vendre_aux_prix WHERE NPRODUIT = '".$_GET['edit_prod_id'] ."' ORDER BY date DESC LIMIT 1;";
			$query5 = "SELECT * FROM emplacements WHERE NPRODUIT = '".$_GET['edit_prod_id'] ."'";
			$query6 = "SELECT * FROM couter WHERE NPRODUIT = '".$_GET['edit_prod_id'] ."'";
			//<option id=echo $data['ncategorie'] selected= if ($ncat == $data['nacteagore']) echo"true";else echo"false">

			$category = mysql_query($query, $connect) or die(mysql_error());
			$cat_list = mysql_query($query2, $connect) or die(mysql_error());
			$prod = mysql_query($query3, $connect) or die(mysql_error());
			$prix = mysql_query($query4, $connect) or die(mysql_error());
			$emplacement = mysql_query($query5, $connect) or die(mysql_error());
			$fourni = mysql_query($query6, $connect) or die(mysql_error());
			$data1 = mysql_fetch_array($category);
			$prod2 = mysql_fetch_array($prod);
			$prix2 = mysql_fetch_array($prix);
			$emplacement2 = mysql_fetch_array($emplacement);
			$fourni2 = mysql_fetch_array($fourni);
			echo "<tr> <td>Nom :</td><td><input type='text' name=prod_name value='".$prod2['MARQUE']."'/></td> </tr>";
			echo "<tr> <td>Categorie:</td>";
			echo "<td>";
			echo "<select onChange='new_cat();' name='prod_cat'>";
			//echo "<select>";
			while ($data = mysql_fetch_array($cat_list))
			{
				if ($data['NCATEGORIE'] == $data1['NCATEGORIE'])
					$sel = "SELECTED";
				else
					$sel = "";
				echo "<option id='".$data['NOMCATEGORIE']."' ".$sel." value=".$data['NCATEGORIE']." >".$data['NOMCATEGORIE']."</option>";
			}
			echo '<option id="new" >----nouvelle categorie----</option>';
			echo "</select>";
			echo "<input class='NewCat' style='display:none'id='i_new' type='text' value='Nouvelle categorie'/>";
			echo "</td> </tr>";
			
				echo "<tr> <td>Prix :</td><td><input type='text' name=prod_price value='".$prix2['PRIX']."'/></td> </tr>";
				echo "<tr> <td>TVA produit :</td><td><input type='text' name=prod_tva   value='".$prix2['TVA']."' /></td> </tr>";
				echo "<tr> <td>Cellule :</td><td><input type='text' name=prod_cel value='".$emplacement2['CELLULE']."' /></td> </tr>";
				echo "<tr> <td>Quantit&eacute; max:</td><td><input type='text' value='".$emplacement2['MAXI']."' name='prod_max'/></td> </tr>";
				echo "<tr> <td>Code produit:</td> <td><input type='text' value='".$prod2['CODEPRODUIT']."' name='prod_code'/></td> </tr>";
				echo "<tr> <td>Nom fournisseur:</td>"; 
				
				$query = "SELECT * FROM fournisseurs";
				$four_list = mysql_query($query, $connect) or die (mysql_error());
				echo "<td>";
				echo "<select style='width:150px' name='prod_four' >";
				while ($data = mysql_fetch_array($four_list))
				{
					if ($data['NFOURNISSEUR'] == $fourni2['NFOURNISSEUR'])
						$sel = "SELECTED";
					else
						$sel = "";
					echo "<option id='".$data['NFOURNISSEUR']."' $sel value=".$data['NOM'].">".$data['NOM']."</option>";
				}
				echo "</select>";
				echo "</td>";
				$query7 = "SELECT * FROM prix_fournisseurs WHERE REF = '".$fourni2['REF']."'";
				$prixFourni = mysql_query($query7, $connect);
				$prixFournisseur = mysql_fetch_array($prixFourni);
				echo "<tr> <td>Prix fournisseur:</td> <td><input type='text' value='".$prixFournisseur['PRIX']."' name='prod_four_price'/></td> </tr>";
				echo "<tr> <td>TVA fournisseur:</td> <td><input type='text' value='".$prixFournisseur['TVA']."' name='prod_tva_four'/></td> </tr>";
				echo "<input type=hidden name=prod_id value=".$_GET['edit_prod_id']." />";
				echo "<input type=hidden name=mag_id value=".$idmag." />";
				echo "<tr><td></td><td><input type='submit' value='Modifier'></td></tr>";
				echo "</table>";
			?>
			<br />
			</form>
		</center>
	</div>
	<h1><center><a href="index.php?page=magasins&show_mag_id=<?php echo $idmag;?>"><img src="img/jamembo-back.png" width="30px" height="30px"/> Retour a la liste des produits</a></center></h1>
</div>
